home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / c / ExtrasLib.lha / ExtrasLib / Source / MaxStrFontLen.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  2.1 KB  |  87 lines

  1. #include <extras/math.h>
  2. #include <proto/graphics.h>
  3. #include <proto/utility.h>
  4. #include <extras/ext_text.h>
  5. #include <clib/extras_protos.h>
  6. #include <string.h>
  7. #include <math.h>
  8.  
  9. /****** extras.lib/gui_MaxStrFontLen ******************************************
  10. *
  11. *   NAME
  12. *       gui_MaxStrFontLen - get the maximum pixel length of a
  13. *       string containg N characters. 
  14. *
  15. *   SYNOPSIS
  16. *       length=gui_MaxStrFontLen(Font, Chars, LowChar, HighChar)
  17. *
  18. *       LONG gui_MaxStrFontLen(struct TextFont *, ULONG, UBYTE, UBYTE);
  19. *
  20. *   FUNCTION
  21. *       This function returns the maximum number of pixels
  22. *       a string with a certain number of characters could
  23. *       occupy.
  24. *
  25. *   INPUTS
  26. *       Font - struct TextFont * previously opened by 
  27. *              OpenFont() or OpenDiskFont().
  28. *       Chars - the max number characters in a string.
  29. *       LowChar - the ASCII number of lowest character
  30. *                 that could be in a string.
  31. *       HighChar - the ASCII number of the highest character
  32. *                  that could be in a string.
  33. *
  34. *   EXAMPLE
  35. *        find the longest pixel length of a number 
  36. *        upto 3 digits 
  37. *       long maxnumberlen;
  38. *       struct TextFont *tf;
  39. *
  40. *       maxnumberlen=gui_MaxStrFontLen(tf,3,'0','9');
  41. *
  42. *   RESULT
  43. *       the pixel length or 0 if the Font parameter
  44. *       is NULL.
  45. *
  46. ******************************************************************************
  47. *
  48. */
  49.  
  50. ULONG gui_MaxStrFontLen(struct TextFont *Font, ULONG Chars, UBYTE LowChar, UBYTE HighChar)
  51. {
  52.   LONG l, maxcharlen=0,kern=0;
  53.   UBYTE c;
  54.  
  55.   if(!Font || !Chars)
  56.     return(NULL);
  57.     
  58.   if(Font->tf_Flags & FPF_PROPORTIONAL)
  59.   {
  60.     UBYTE s,lo,hi;
  61.     lo=Font->tf_LoChar;
  62.     hi=Font->tf_HiChar;
  63.  
  64.     for(c=LowChar; c<=HighChar; c++)
  65.     {
  66.       s=c;
  67.       if(s>=lo && s<=hi)
  68.         s-=lo;
  69.       else
  70.         s=hi-lo+1;
  71.         
  72.       l=((WORD *)Font->tf_CharSpace)[s]+((WORD *)Font->tf_CharKern)[s];
  73.       if(l>maxcharlen)
  74.       {
  75.         maxcharlen=l;
  76.         kern=((WORD *)Font->tf_CharKern)[s];
  77.       }
  78.     }
  79.   }
  80.   else
  81.   {
  82.     maxcharlen=Font->tf_XSize;
  83.   } 
  84.   
  85.   return(maxcharlen * Chars - kern);
  86. }
  87.